package outil;

public class Si implements Action {

    private final Predicat pPredicat;
    private final Action pAction1,  pAction2;

    public final static Action instance(Predicat pPredicat, Action pAction1, Action pAction2) {

        return new Si(pPredicat, pAction1, pAction2);
    }

    public final static Action instance(Predicat pPredicat, Action pAction1) {

        return new Si(pPredicat, pAction1);
    }

    public Si(Predicat pPredicat, Action pAction1) {
        this.pPredicat = pPredicat;
        this.pAction1 = pAction1;
        this.pAction2 = null;
    }

    private Si(Predicat pPredicat, Action pAction1, Action pAction2) {
        this.pPredicat = pPredicat;
        this.pAction1 = pAction1;
        this.pAction2 = pAction2;
    }

    @Override
    public void exec() {
        if (pPredicat.eval() == true) {
            pAction1.exec();
        } else {
           if (pAction2!=null) pAction2.exec();
        }
    }
}
